package com.yanze.schoolinformation.view.my;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yanze.schoolinformation.R;
import com.yanze.schoolinformation.app.AppConfig;
import com.yanze.schoolinformation.app.C;
import com.yanze.schoolinformation.base.BaseFragment;
import com.yanze.schoolinformation.databinding.FragmentMeBinding;
import com.yanze.schoolinformation.helper.ImageHelper;
import com.yanze.schoolinformation.model.bean.resp.RespUser;
import com.yanze.schoolinformation.utils.DialogUtils;
import com.yanze.schoolinformation.utils.FilePathManager;
import com.yanze.schoolinformation.utils.PerfectClickListener;
import com.yanze.schoolinformation.utils.SPUtils;
import com.yanze.schoolinformation.utils.ToastUtils;
import com.yanze.schoolinformation.view.login_register.LoginActivity;
import com.yanze.schoolinformation.view.my.person.AboutActivity;
import com.yanze.schoolinformation.view.my.person.IssueActivity;
import com.yanze.schoolinformation.view.my.person.PersonActivity;
import com.yanze.schoolinformation.view.my.person.VpnActivity;

import java.io.File;

/**
 * Created by MrZheng on 2018/3/14.
 */

public class MyFragment extends BaseFragment<FragmentMeBinding> implements View.OnClickListener {

    private boolean isHidden;
    private boolean isLogin;
    private RespUser.UserInfo mUser;
    private Dialog dialog;

    @Override
    public int setContent() {
        return R.layout.fragment_me;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        showContentView();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        isHidden = hidden;
        if (!isHidden) {
            initView();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!isHidden) {
            initView();
        }
    }

    private void initView() {
        mUser = AppConfig.getUserInfo(getContext());
        isLogin = mUser == null ? false : true;
        if (isLogin) {
            ImageHelper.displayCircle(bindingView.ivHeader, C.url.API_IMAGE + mUser.getUsericon());
            bindingView.tvNick.setText(mUser.getNickname());
        } else {
            Glide.with(getActivity()).load(R.mipmap.person_icon).dontAnimate().into(bindingView.ivHeader);
            bindingView.tvNick.setText("登录/注册");
        }
        MyOnClick myOnClick = new MyOnClick();
        bindingView.ivHeader.setOnClickListener(myOnClick);
        bindingView.rlClear.setOnClickListener(myOnClick);
        bindingView.rlIssue.setOnClickListener(myOnClick);
        bindingView.rlAbout.setOnClickListener(myOnClick);
        bindingView.rlVpnip.setOnClickListener(myOnClick);
        bindingView.rlUnlogin.setOnClickListener(myOnClick);
    }

    private class MyOnClick extends PerfectClickListener {

        @Override
        protected void onNoDoubleClick(View v) {
            switch (v.getId()) {
                case R.id.iv_header:
                    if (isLogin) {
                        PersonActivity.start(getContext(), mUser);
                    } else {
                        LoginActivity.start(getContext());
                    }
                    break;
                case R.id.rl_clear:
                    clearCache();
                    break;
                case R.id.rl_issue:
                    IssueActivity.start(getContext());
                    break;
                case R.id.rl_about:
                    AboutActivity.start(getContext());
                    break;
                case R.id.rl_vpnip:
                    VpnActivity.start(getContext());
                    break;
                case R.id.rl_unlogin:
                    if (isLogin) {
                        AlertDialog.Builder dialog = new AlertDialog.Builder(getContext());
                        dialog.setMessage("是否退出登录？");
                        dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                loginout();
                            }
                        });
                        dialog.setNegativeButton("取消", null);
                        dialog.show();
                    }
                    break;
            }
        }
    }

    private void loginout() {
        File userCache = FilePathManager.getUserCachePath(getContext());
        if (userCache != null) {
            userCache.delete();
        }
        SPUtils.put(getContext(), "userid", "");
        DialogUtils.showDialog(getContext(), "正在注销...");
        bindingView.ivHeader.postDelayed(new Runnable() {
            @Override
            public void run() {
                Glide.with(getActivity()).load(R.mipmap.person_icon).dontAnimate().into(bindingView.ivHeader);
                bindingView.tvNick.setText("登录/注册");
                isLogin = false;
                DialogUtils.dismissDialog();
                ToastUtils.show(getContext(), "注销成功");
            }
        }, 500);
    }

    private void clearCache() {
        //清楚缓存
        if (dialog == null) {
            dialog = new Dialog(getContext(), R.style.DiyDialog);

            View clearView = LayoutInflater.from(getContext()).inflate(R.layout.dialog_cacle, null);
            clearView.findViewById(R.id.clear_cache_canner).setOnClickListener(this);
            clearView.findViewById(R.id.clear_cache_sure).setOnClickListener(this);
            dialog.setContentView(clearView);
        }
        dialog.show();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.clear_cache_sure:
                if (dialog != null) {
                    dialog.dismiss();
                }
                ToastUtils.show(getActivity(), "假装清理缓存");
                break;
            case R.id.clear_cache_canner:
                if (dialog != null) {
                    dialog.dismiss();
                }
                break;
        }
    }
}
